﻿using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections.Generic;
using System;

namespace TinyAsset.Editor
{
    public class BuildPrepareTask : IBuildTask
    {
        BuildSetting buildSetting;

        public bool Execute(BuildContext context)
        {
            buildSetting = context.buildSetting;

            if (!EditorUserBuildSettings.SwitchActiveBuildTarget(buildSetting.buildTargetGroup,buildSetting.buildTarget))
                return false;

            EditorUserBuildSettings.development = buildSetting.isDevelopmentBuild;
            EditorUserBuildSettings.allowDebugging = buildSetting.isDevelopmentBuild;
            EditorUserBuildSettings.connectProfiler = buildSetting.isDevelopmentBuild;

            if (context.AppVersion == null)
                context.AppVersion = buildSetting.buildHistoryRegistry.GetNextAppVerion(buildSetting.buildTarget);

            if (context.ResVersion == null)
                context.ResVersion = buildSetting.buildHistoryRegistry.GetNextResVersion(buildSetting.buildTarget);

            string platform = AssetBundleUtility.GetPlatformName();
            string buildResFolder = Path.Combine(buildSetting.output, Builder.kStrRes, platform);
            string buildAppFolder = Path.Combine(buildSetting.output, Builder.kStrApp, platform, context.AppVersion.ToString());

            context.buildAssetBundlePath = Path.Combine(buildResFolder, context.ResVersion.ToString(), AssetBundleUtility.kAssetBundle);
            if (!Directory.Exists(context.buildAssetBundlePath))
                Directory.CreateDirectory(context.buildAssetBundlePath);

            if (!buildSetting.forceRebuildAssetBundle)
            {
                Version current = buildSetting.buildHistoryRegistry.GetCurrentResVersion(buildSetting.buildTarget);
                if (current != null)
                {
                    string assetBundleBuildPath = Path.Combine(buildResFolder, current.ToString(), AssetBundleUtility.kAssetBundle);
                    if (Directory.Exists(assetBundleBuildPath))
                        FileHelper.CopyFolder(assetBundleBuildPath, context.buildAssetBundlePath, null);
                }
            }

            if (buildSetting.isBuildPlayer)
            {
                PlayerSettings.bundleVersion = context.AppVersion.ToString();
                PlayerSettings.Android.bundleVersionCode = BuildHistoryRegistry.GetVersionNumber(context.AppVersion);
                PlayerSettings.iOS.buildNumber = context.AppVersion.ToString();

                PlayerSettings.companyName = context.companyName;
                PlayerSettings.productName = context.productName;

                if (Directory.Exists(Application.streamingAssetsPath))
                    Directory.Delete(Application.streamingAssetsPath, true);
            }

            context.ManifestLocation = Path.Combine(buildResFolder, context.ResVersion.ToString(), AssetBundleUtility.kManifest);
            context.PlayerLocation = Path.Combine(buildAppFolder, string.Format("{0}{1}", context.productName, GetTargetExtension(buildSetting.buildTarget)));
            context.buildResFolder = buildResFolder;

            BuildOptions developmentBuildOptions =
                BuildOptions.Development |
                BuildOptions.AllowDebugging |
                BuildOptions.ConnectToHost |
                BuildOptions.ConnectWithProfiler;

            if (buildSetting.isDevelopmentBuild)
                buildSetting.buildOptions |= developmentBuildOptions;
            else
                buildSetting.buildOptions &= ~developmentBuildOptions;

            if (buildSetting.forceRebuildAssetBundle)
                buildSetting.buildAssetBundleOptions |= BuildAssetBundleOptions.ForceRebuildAssetBundle;
            else
                buildSetting.buildAssetBundleOptions &= ~BuildAssetBundleOptions.ForceRebuildAssetBundle;

            PlayerSettings.SetScriptingBackend(buildSetting.buildTargetGroup, buildSetting.scriptingImplementation);
            return true;
        }

        string GetTargetExtension(BuildTarget buildTarget)
        {
            string extensions = string.Empty;

            switch (buildTarget)
            {
                case BuildTarget.StandaloneWindows:
                case BuildTarget.StandaloneWindows64:
                    extensions = ".exe";
                    break;
                case BuildTarget.Android:
                    extensions = ".apk";
                    break;
            }

            return extensions;
        }
    }
}